#pragma once

#include "TestScene.h"
#include "MyGUI.h"
#include "MyGUI_EggPlatform.h"
using namespace MyGUI;

class TestGUI : public TestScene{
public:
    void render(){
        mRenderer->render(mCamera);
        mPlatform->getRenderManagerPtr()->drawOneFrame();
    }

    void onMouseMove(int x,int y){
        mInputMgr->injectMouseMove(x,y,0);
    }
protected:
    void setupGUI(){
        mPlatform=new EggPlatform();
        mPlatform->initialise("MyGUI.log");
        mPlatform->getRenderManagerPtr()->setViewSize(mWidth,mHeight);
        mPlatform->getDataManagerPtr()->addResourceLocation("./media",false);
        mGUI=new Gui();
        mGUI->initialise("MyGUI_Core.xml");
        mInputMgr=InputManager::getInstancePtr();
        LayoutManager::getInstance().loadLayout("grafter.layout");
    }

    void setupCamera(){
        mCamera=new Camera(mScene);
        mCamera->setFar(10);
        mCamera->setPosition(vec3(0,0,4),TS_PARENT);
        mRootNode->attachChild(mCamera);
        Viewport* vp=mCamera->getViewport();
        vp->mClearColor=vec4(0.3,0.2,0.1,0);
        vp->setRect(0,0,mWidth,mHeight);
    }

    void setupDisplay(){
        ShaderPtr simple(new Shader());
        simple->fromFile("glsl/simple.vs","glsl/simple.fs");
        MaterialPtr mat(new Material(simple));
        mat->setShininess(50.0);
        mat->setColor(vec4(1,0,0,1));

        SceneUtil ss;
        GeometryMap geos;
        ss.loadGeometries("media/teapot.obj",geos);
        GeometryPtr sphere=geos.begin()->second;
        sphere->updateTangent();

        MeshEntity* ent=new MeshEntity();
        ent->setGeometry(sphere);
        ent->setMaterial(mat);
        mNode=new SceneNode();
        mNode->attachEntity(ent);
        mRootNode->attachChild(mNode);

        sphere->updateAabb();
        const Aabb& box=sphere->getAabb();
        mAabb=new AabbEntity();
        mAabb->setAabb(box);
        mAabb->setColor(vec4(1,0,0,1));
        mNode->attachEntity(mAabb);
    }
protected:
    Camera* mCamera;
    SceneNode* mNode;
    AabbEntity* mAabb;
    EggPlatform* mPlatform;
    InputManager* mInputMgr;
    Gui* mGUI;
};